package com.mall.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mall.common.utils.Response;
import com.mall.common.utils.ResponseMessage;
import com.mall.vo.WebResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.mall.entity.Category;
import com.mall.service.CategoryService;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.R;



/**
 * 商品三级分类
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-08-24 17:44:27
 */
@RestController
@RequestMapping("product/category")

@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @GetMapping("/list/tree")
    //@RequiresPermissions("product:category:list")
    public Response listWithTree(){
        List<Category> categoryList= categoryService.listWithTree();
        return new Response(ResponseMessage.SUCCESS).data(categoryList);
    }


    @GetMapping("/list/tree01/{id}")
    public Response listTree(@PathVariable Integer id){
//        List<Category> categoryList= categoryService.listWithTree();
//        Response response;
//        if (categoryList!=null){
//            response = new Response(ResponseMessage.SUCCESS).data(categoryList);
//        }else {
//            response = new Response(ResponseMessage.ERROR);
//        }
        IPage<Category> categoryIPage = categoryService.pageList(id, 5);
        return new Response(ResponseMessage.SUCCESS).data(categoryIPage);
    }







    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    // @RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
        Category category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:category:save")
    public R save(@RequestBody Category category){
        categoryService.save(category);

        return R.ok();
    }

    @PostMapping("/add")
    public Response add(@RequestBody Map<String, String> map){
        String name = map.get("name");
        Integer parentCid = Integer.valueOf(map.get("parentCid"));
        Integer catLevel = Integer.valueOf(map.get("catLevel"));
        Integer showStatus = Integer.valueOf(map.get("showStatus"));
        Integer sort = Integer.valueOf(map.get("sort"));

        Category category = new Category();

        category.setCatId(null);
        category.setName(name);
        category.setParentCid(Long.valueOf(parentCid));
        category.setCatLevel(catLevel);
        category.setShowStatus(showStatus);
        category.setSort(sort);
//        category.setIcon();
//        category.setProductUnit();
//        category.setProductCount();
//        category.setChildren();
        boolean save = categoryService.save(category);
        return new Response(ResponseMessage.SUCCESS);
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:category:update")
    public R update(@RequestBody Category category){
        categoryService.updateDetail(category);
       // categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Response delete(@Param("id") Integer id){
        categoryService.removeMenusByIds(id);
        return new Response(ResponseMessage.SUCCESS);
    }


    //  @RequiresPermissions("product:category:delete")
//    public R delete(@RequestBody Long[] catIds){
////        categoryService.removeByIds(Arrays.asList(catIds));
//        System.out.println(catIds);
//        categoryService.removeMenusByIds(Arrays.asList(catIds));
//        return R.ok();
//    }

}
